#!/usr/binenv python
import sys
import numpy

def main():
    if len(sys.argv) < 4:
        sys.stderr.write('%s <train> <train_answers> <test>' % (sys.argv[0]))
        exit(1)

    train_path          =   sys.argv[1]
    train_answers_path  =   sys.argv[2]
    test_path           =   sys.argv[3]

    plus_amplification  =   3.0
    minus_amplification =   2.0

    answers =   list()
    with open(train_answers_path, 'r') as train_answers_file:
        for x in train_answers_file:
            value   =   int(x)
            koeff   =   minus_amplification
            if 1 == value:
                koeff   =   plus_amplification
            answers.append(value * koeff)

    examples    =   list()
    with open(train_path, 'r') as train_file:
        for line in train_file:
            example =   [float(x) for x in line.split(' ')]
            """free member"""
            example.append(1.0)
            koeff   =   minus_amplification
            if 1 == answers[len(examples)]:
                koeff   =   plus_amplification
            example =   [koeff * x for x in example]
            examples.append(example)


    teta    =   numpy.linalg.lstsq(examples, answers)[0]

    with open(test_path, 'r') as test_file:
        for line in test_file:
            example =   [float(x) for x in line.split(' ')]
            """free member"""
            example.append(1.0)
            dotproduct          =   numpy.dot(example, teta)
            diff_to_one         =   abs(1 - dotproduct)
            diff_to_minus_one   =   abs(-1 - dotproduct)
            result              =   -1
            if diff_to_one < diff_to_minus_one:
                result  =   1
            print result

if __name__ == "__main__":
    main()
